$(function(){

/* SIMULATOR */
$('.trunk_selection, .codec_selection, #button-simulate, .button-simulate').hide();

$('#carrier_selection').change(function(){
	load_simulator_trunks(this);
});
$('#trunk_selection').change(function(){
	load_simulator_codecs(this);
});
$('#codec_selection').change(function(){

	if($(this).val() == 'none')
	{
		$('#button-simulate, .button-simulate').hide();
	}
	else
	{
		$('#button-simulate, .button-simulate').show();	
	}
});

$( "#button-simulate" )
			.button()
			.click(function() {
				simulate();
			});
			
create_virtual_links();


if(window.location.hash.split("#")[1] != "" && window.location.hash.split("#")[1] != undefined)
{
	id = "#anchor_" + window.location.hash.split("#")[1];
	
	$(id).addClass('ui-state-highlight');
}

$("#module-carriers-table").find('tr')
	.mouseover(function(){
	$(this).addClass('ui-state-hover');
	})
	.mouseout(function(){
	$(this).removeClass('ui-state-hover');
	});

$( ".open-event" ).tooltip({
			show: null,
			position: {
				my: "left top",
				at: "left bottom"
			},
			open: function( event, ui ) {
				ui.tooltip.animate({ top: ui.tooltip.position().top + 10 }, "fast" );
			}
		});

$( ".hidden-trunk").each(function(){

	$(this).hide();

});


$( ".hidden-ip").each(function(){

	$(this).hide();

});

$( ".trunk" ).change(function() {

	activate_short_name_field();

});

$( ".sample-row" ).hide();

$( "#add-row" )
			.button()
			.click(function() {
				window.document.location.href = siteUrl+'/origin/add';
			});


$( "#module-add-origin-trunk" )
			.button()
			.click(function() {
				window.document.location.href = siteUrl+'/origin/add';
			});

$( "#module-add-family" )
			.button()
			.click(function() {
				window.document.location.href = siteUrl+'/families/add';
			});

			
$( "#module-add-termination-trunk" )
			.button()
			.click(function() {
				window.document.location.href = siteUrl+'/termination/add';
			});
			
// save settings	
$( "#module-save-settings" )
			.button()
			.click(function() {	
                                save_settings();                                
			});
			
$( "#module-edit-origin-trunk" )
			.button()
			.click(function() {	
								$( "#short_name_status" ).html('');
                                edit_trunk(trunk_ID,"origin");
			});

$( "#module-route-origin-trunk" )
			.button()
			.click(function() {	
								$( "#short_name_status" ).html('');
                                route_origin_trunk(trunk_ID,"origin");
			});
			
$( "#module-route-cancel" )
			.button()
			.click(function() {	
								window.location.reload();
			});			
			
$( "#module-edit-termination-trunk" )
			.button()
			.click(function() {	
								$( "#short_name_status" ).html('');
                                edit_trunk(trunk_ID,"termination");
			});
			
$( "#module-save-origin-trunk" )
			.button()
			.click(function() {	
								$( "#short_name_status" ).html('');
                                save_new_trunk("origin");
			});			

$( "#module-save-family" )
			.button()
			.click(function() {	
								$( "#short_name_status" ).html('');
                                save_new_family();
			});			
		
$( "#module-save-edit-family" )
			.button()
			.click(function() {	
								$( "#short_name_status" ).html('');
                                save_edit_family();
			});			
			
$( "#module-save-termination-trunk" )
			.button()
			.click(function() {	
								$( "#short_name_status" ).html('');
                                save_new_trunk("termination");
			});			
			
$( "#module-edit-trunk" )
			.button()
			.click(function() {	
								$( "#short_name_status" ).html('');
                                edit_trunk(trunk_ID);
			});

			
$( "#short_name" )
			.keypress(function(){			
				var text = $( "#short_name" ).val().toUpperCase();				
				$( "#short_name" ).val(text);			
			}).keyup(function(){			
				var text = $( "#short_name" ).val().toUpperCase();				
				$( "#short_name" ).val(text);		
				check_short_name();
			});

$( "#family_short_name" )
			.keypress(function(){			
				var text = $( "#family_short_name" ).val().toUpperCase();				
				$( "#family_short_name" ).val(text);			
			}).keyup(function(){			
				var text = $( "#family_short_name" ).val().toUpperCase();				
				$( "#family_short_name" ).val(text);		
				check_family_short_name();
			});

$( "#family" ).change(function(){

	if($( "#family" ).val() != "none")
	{
		load_trunks($( "#family" ).val());
	}

});	
			

$( "#contract_type" ).change(function(){

change_contract_type();

});	

// deactivate selected elements	
$('.deactivated').click(function(){
				$(this).blur();
			}).focus(function(){
				$(this).blur();
			}).select(function(){
				$(this).blur();
			}).change(function(){
				$(this).blur();
			});		

// Dialog			
$('#enable').dialog({
				autoOpen: false,
				width: 600,
				buttons: {
						"Yes": function() { 
							enable_trunk(trunk_ID,$(this));
						}, 
						"No": function() { 
							$(this).dialog("close"); 
						} 
				}
});
$('#disable').dialog({
				autoOpen: false,
				width: 600,
				buttons: {
						"Yes": function() { 
							disable_trunk(trunk_ID,$(this));
						}, 
						"No": function() { 
							$(this).dialog("close"); 
						} 
				}
});
	
$('#disable-ip-address').dialog({
				autoOpen: false,
				width: 600,
				buttons: {
						"Yes": function() { 
							
							//alert(ip_ID);
						
							disable_ip(ip_ID,$(this));
						}, 
						"No": function() { 
							$(this).dialog("close"); 
						} 
				}
});

$('#link-trunk').dialog({
				autoOpen: false,
				width: 400,
				height: 400,
				modal: true,
				buttons: {
						"Done": function() { 
							
							//alert(ip_ID);
						
							link_trunks();
						}, 
						"Cancel": function() { 
							$(this).dialog("close"); 
						} 
				}
});

$('#unlink-trunk').dialog({
				autoOpen: false,
				width: 600,
				buttons: {
						"Confirm": function() { 
							unlink_trunk();
							$(this).dialog("close"); 
						},
						"Cancel": function() { 
							$(this).dialog("close"); 
						}						
				}
});

$('#view_trunks').dialog({
				autoOpen: false,
				width: 400,
				height: 400,
				modal: true,
				buttons: {
						"Ok": function() { 
							$(this).dialog("close"); 
						} 
				}
});


$('#link-trunks-button').click(function(){
	$('#link-trunk').dialog( "open" );
});

$( ".routing").each(function(){

	$(this).button().addClass('options-button');
	
	var status = $(this).attr("rel");
	var trunk_id = $(this).attr("name");

	if(status == 0)
	{
		$(this).removeClass('ui-state-disabled').addClass('ui-state-highlight');
	}

	else if(status == 1)
	{
		$(this).addClass('ui-state-default');
	}

	$(this).click(function(){
			window.document.location.href = 'routing/'+trunk_id;
	});
	
});


$( ".edit").each(function(){

	$(this).button().addClass('options-button');
	
	var status = $(this).attr("rel");
	var trunk_id = $(this).attr("name");

	if(status == 0)
	{
		$(this).removeClass('ui-state-disabled').addClass('ui-state-highlight');
	}

	else if(status == 1)
	{
		$(this).addClass('ui-state-default');
	}

	$(this).click(function(){
			window.document.location.href = 'edit/'+trunk_id;
	});
	
});

$('#delete').dialog({
				autoOpen: false
				});

$('#deletion_not_allowed').dialog({
				autoOpen: false
				});
				
				
$( ".edit_family").each(function(){

	$(this).button().addClass('options-button');
	
	var family_id = $(this).attr("name");

	$(this).addClass('ui-state-default');

	$(this).click(function(){
			window.document.location.href = siteUrl + '/families/edit/'+family_id;
	});
	
});

$( ".delete_family").each(function(){

	$(this).button().addClass('options-button');
	
	var family_id = $(this).attr("name");
	
	var family_name = $(this).attr("rel");	

	$(this).addClass('ui-state-default');

	$(this).click(function(){
			confirm_delete_family(this,family_id,family_name);
	});
	
});

$("#enable, #disable").hide();	
$(".status_0").addClass('ui-state-disabled');
	
$( ".options").each(function(){

	$(this).button().addClass('options-button');
	
	var status = $(this).attr("rel");
	var trunk_name = $(this).attr("title");
	var trunk_id = $(this).attr("id");

	
	if(status == 0)
	{
		$(this).removeClass('ui-state-disabled').addClass('ui-state-highlight');
	}

	else if(status == 1)
	{
		$(this).addClass('ui-state-default');
	}

	$(this).click(function(){
	
	var status = $(this).attr("rel");
	
			if(status == 0)
			{
				trunk_ID = trunk_id;
				$('.trunk_name').html(trunk_name);
				$('#enable').dialog( "open" );							
			}
		
			else if(status == 1)
			{
				trunk_ID = trunk_id;
				$('.trunk_name').html(trunk_name);			
				$('#disable').dialog( "open" );							
			}			
	});
	
});
	
	/* SEARCH TRUNKS */
	
	// submit button action
	$("#filter-submit").button();
	
	// replace value of the field
	$("#short_name_field").click(function(){	
	
		if($(this).val() == term_short_name)
		{
			$(this).val("");
		}
	});	
});